package com.coocaa.ops.admin.v1.dict.convert;

import com.coocaa.ops.admin.base.util.WebUtil;
import com.coocaa.ops.common.core.biz.auth.entity.SysUserEntity;
import com.coocaa.ops.common.core.biz.dict.entity.DictInfoEntity;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import lombok.experimental.UtilityClass;
import org.apache.commons.collections4.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * @author bijiahao
 * @description
 * @date 2020/7/8 10:26
 */
@UtilityClass
public class DictQueryConverter {

    public static List<DictInfoEntity> handleBusinessType(HttpServletRequest request, List<DictInfoEntity> types, Integer filter) {
        if (Objects.equals(0, filter)){
            //用户新增那里使用0
            return types;
        }
        SysUserEntity user = WebUtil.getCurrentUser(request);
        //相关业务组信息上来
        List<Integer> dicts = user.getDicts();
        List<DictInfoEntity> result = InstanceUtil.newArrayList();
        if (CollectionUtils.isEmpty(dicts)) {
            return result;
        }
        for (DictInfoEntity entity : types) {
            if (dicts.contains(entity.getId())) {
                result.add(entity);
            }
        }
        return result;
    }
}
